﻿/**
 * Copyright (C) 2004-2009 D3Systems, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace OMGGateways
{
    /// <summary>
    /// Helper Class for reading and writing into the OMG Config file (omg.xml)
    /// that should be found in the User's Directory inside the 
    /// .omg directory.
    /// </summary>
    public class ConfigurationHandler
    {
        public static String configFilePath;
        public static String configDirPath;

        public static String checkDir()
        {
            String appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            if(configDirPath == null)
                configDirPath = Path.Combine(appDataPath, ".omg");

            DirectoryInfo dirInfo = new DirectoryInfo(configDirPath);

            String str = "";

            try
            {
                str = dirInfo.ToString();
                if (!dirInfo.Exists)
                {
                    dirInfo.Create();
                }
                
            }
            catch (Exception ex)
            {
                str = ex.ToString();
            }

            return str;
            
        }

        public static FileInfo checkFile()
        {
            if(configFilePath == null)
                configFilePath = Path.Combine(configDirPath, "omg.xml");

            FileInfo file = new FileInfo(configFilePath);

            if (!file.Exists)
                file.Create();

            return file;
        }

        public static String ReadConfigFile()
        {
            String result = "";

            try
            {
                Boolean isAccounts = false;
                LoginDetails loginAcct = null;
                
                XmlTextReader objXmlTextReader =
                      new XmlTextReader(configFilePath);

                string sName = "";
                while (objXmlTextReader.Read())
                {
                    switch (objXmlTextReader.NodeType)
                    {
                        case XmlNodeType.Element:                            
                            sName = objXmlTextReader.Name;                            
                            switch (sName)
                            {
                                case "accounts":
                                    isAccounts = true;
                                    break;
                                case "account":
                                    loginAcct = new LoginDetails();
                                    break;
                            }
                            break;
                        case XmlNodeType.Text:
                            switch (sName)
                            {
                                case "username":
                                    loginAcct.Username = objXmlTextReader.Value;
                                    break;
                                case "password":
                                    loginAcct.Password = objXmlTextReader.Value;
                                    break;
                                case "domain":
                                    loginAcct.Domain = objXmlTextReader.Value;
                                    break;
                                case "port":
                                    loginAcct.Port = Int32.Parse(objXmlTextReader.Value);
                                    break;
                                case "assembly":
                                    loginAcct.AssemblyName = objXmlTextReader.Value;
                                    break;
                                case "class":
                                    loginAcct.ClassName = objXmlTextReader.Value;
                                    break;
                                case "type":
                                    loginAcct.GatewayType = Int32.Parse(objXmlTextReader.Value);
                                    break;
                            }
                            break;
                        case XmlNodeType.EndElement:
                            string endElemName = objXmlTextReader.Name;
                            switch (endElemName)
                            {
                                case "account":
                                    GWController.getInstance().RegisterGateway(loginAcct);
                                    break;
                            }
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                result += ex.ToString();
            }

            return result;
        }
    }
}
